package org.marsdonne.sync;

import com.fasterxml.jackson.databind.JsonNode;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

@SyncedImpl(domain = "test")
public class SyncedSampleImpl implements Synced {

    private Set<Long> createdIdempotent = new HashSet<>();
    private Set<Long> changedIdempotent = new HashSet<>();
    private Set<Long> deletedIdempotent = new HashSet<>();

    private static CountDownLatch COUNT_CHANGED;
    private static CountDownLatch COUNT_CREATED;
    private static CountDownLatch COUNT_DELETED;

    public static void initCounter(int creatable, int changeable, int deletable) {
        COUNT_CHANGED = new CountDownLatch(changeable);
        COUNT_CREATED = new CountDownLatch(creatable);
        COUNT_DELETED = new CountDownLatch(deletable);
    }

    public static CountDownLatch getCountChanged() {
        return COUNT_CHANGED;
    }

    public static CountDownLatch getCountCreated() {
        return COUNT_CREATED;
    }

    public static CountDownLatch getCountDeleted() {
        return COUNT_DELETED;
    }

    @Override
    public void created(Date dateSynced, JsonNode content, long version) {
        if (!createdIdempotent.contains(version))
            COUNT_CREATED.countDown();
    }

    @Override
    public void changed(Date dateSynced, JsonNode content, long version) {
        if (!changedIdempotent.contains(version))
            COUNT_CHANGED.countDown();
    }

    @Override
    public void deleted(Date dateSynced, JsonNode content, long version) {
        if (!deletedIdempotent.contains(version))
            COUNT_DELETED.countDown();
    }
}
